package jspeech.sample;

import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.util.Vector;
import jspeech.SpeechRecognizer;
import jcommand.Action;
import jcommand.Command;
import jcommand.CommandSet;

/**
 * Sample of {@link jspeech.SpeechRecognizer}. This demo is using your voice to
 * give commands to a game (Total War).
 * 
 * @author Kenneth Lo
 * 
 */
public class TotalWar {

	public static void main(String[] args) {

		String lastCommand = "";

		try {

			// Define command
			CommandSet commandSet = new CommandSet();
			Vector<Command> commands = new Vector<Command>(10, 5);

			commands.add(new Command("General"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_1));

			commands.add(new Command("Archers"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_2));

			commands.add(new Command("Team One"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_3));

			commands.add(new Command("Team Two"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_4));

			commands.add(new Command("Attrack"));
			commands.lastElement().addAction(
					Action.MouseClick(InputEvent.BUTTON3_MASK));

			commands.add(new Command("Stop"));
			commands.lastElement().addAction(
					Action.KeyClick(KeyEvent.VK_BACK_SPACE));

			commands.add(new Command("Withdraw"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_W));

			commands.add(new Command("Formation"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_C));

			commands.add(new Command("Running Mode"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_R));

			commands.add(new Command("Grouping"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_G));

			commands.add(new Command("Go Here"));
			commands.lastElement().addAction(
					Action.MouseClick(InputEvent.BUTTON3_MASK));

			commands.add(new Command("Confirm"));
			commands.lastElement().addAction(
					Action.MouseClick(InputEvent.BUTTON1_MASK));

			commands.add(new Command("Grard Mode"));
			commands.lastElement().addAction(Action.KeyClick(KeyEvent.VK_D));

			commands.add(new Command("Select All"));
			commands.lastElement().addAction(
					Action.KeyHoldAndClick(KeyEvent.VK_CONTROL, KeyEvent.VK_A));

			for (int i = 0; i < commands.size(); i++)
				commandSet.addCommand(commands.elementAt(i));

			// End of Define command
			SpeechRecognizer sr = new SpeechRecognizer(commandSet
					.getCommandKeys(), "JSpeech.xml");
			sr.start();
			while (sr.isRunning()) {
				String command = sr.recognizedCommand();
				commandSet.runCommand(command, lastCommand);
				lastCommand = command;
			}
			sr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
